var baseUrl = 'http://101.132.100.147:8080/spider-core/';

$(function () {
    var pageSize = 5;
    var pageNum = 1;
    var loading = false;
    var hasNextPage = false;

    var loadImageDir = function(){
        $.ajax( {
            url:baseUrl + 'queryUUImageDir',// 跳转到 action
            data:{
                pageNum:pageNum,
                pageSize:pageSize
            },
            type:'post',
            cache:true,
            crossDomain: true,
            dataType:'json',
            success:function(data) {
                $('#myTemplate').tmpl(data.list).appendTo('.imageDir');
                hasNextPage = data.hasNextPage;
            },
            error : function() {
            }
        });
        pageNum ++ ;
    }

    var pullToRefreshImageDir = function(){
        $.ajax( {
            url:baseUrl + 'queryUUImageDir',// 跳转到 action
            data:{
                pageNum:1,
                pageSize:5
            },
            type:'post',
            cache:true,
            crossDomain: true,
            dataType:'json',
            success:function(data) {
                $('.imageDir').html('');
                $('#myTemplate').tmpl(data.list).appendTo('.imageDir');
            },
            error : function() {
            }
        });
    }

    var loadImageDetail = function(sid){
        var imageDetial = new Array();
        $.ajax( {
            url:baseUrl + 'queryUUImageDetail',// 跳转到 action
            data:{
                sid:sid,
                pageNum:1,
                pageSize:100
            },
            type:'post',
            async:false,
            cache:true,
            crossDomain: true,
            dataType:'json',
            success:function(data) {
                var list = data.list;
                for(var item in list) {
                    imageDetial.push(list[item].imgurl);
                }
            },
            error : function() {
            }
        });
        return imageDetial;
    }

    loadImageDir();

    $(document).on('infinite', '.infinite-scroll',function() {
        // 如果正在加载，则退出
        if (loading) return;
        // 设置flag
        loading = true;
        setTimeout(function() {
            loading = false;
            if (!hasNextPage) {
                $.detachInfiniteScroll($('.infinite-scroll'));
                $('.infinite-scroll-preloader').remove();
                return;
            }else{
                loadImageDir();
            }
        }, 200);
    });

    $(document).on('refresh', '.pull-to-refresh-content',function(e) {
            pullToRefreshImageDir();
            $.pullToRefreshDone('.pull-to-refresh-content');
    });

    //点击时打开图片浏览器
    $(document).on('click','.card',function (e) {
        var sid = e.target.id;
        var photes = loadImageDetail(sid)
        var photoBrowserStandalone = $.photoBrowser({
            photos : photes,
            theme: 'dark',
            type: 'standalone'
        });
        photoBrowserStandalone.open();
    });

    $.init();
});